/**
**************************************************************************************
*Founder Effect                                                                      *
*Copyright (C) 2006-2007, 5d Studios (www.5d-Studios.com)                            *
*                                                                                    *
*This library is free software; you can redistribute it and/or                       *
*modify it under the terms of the GNU Lesser General Public                          *
*License as published by the Free Software Foundation; either                        *
*version 2.1 of the License, or (at your option) any later version.                  *
*                                                                                    *
*This library is distributed in the hope that it will be useful,                     *
*but WITHOUT ANY WARRANTY; without even the implied warranty of                      *
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                   *
*Lesser General Public License for more details.                                     *
*                                                                                    *
*You should have received a copy of the GNU Lesser General Public                    *
*License along with this library; if not, write to the Free Software                 *
*Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA      *
**************************************************************************************
*/
#include "StdAfx.h"

#include "../Kernel.h"

#include "SceneManager.h"

extern Kernel* g_kernel;


/*-----------------------------------------------------------------------------------------------------------------
    Map Change command
------------------------------------------------------------------------------------------------------------------*/

void MapChange_f::Exec(std::string &s)
{
    if ( sceneMng )
    {
        // attemp to load the scene
        sceneMng->LoadMap( s ); 

    }
}

/**
============================
Constructor
============================
*/
SceneManager::SceneManager(void): m_currentMap(NULL), m_loadingMap(false), m_mapLoaded(false)
{ 
    m_mapChange.Set( this );
    g_kernel->GetConsole()->AddCommand( &m_mapChange, "map" );
}

/**
============================
Load a scene into memory
============================
*/
bool  SceneManager::LoadMap( const std::string &file )
{  
    // free old scene
    UnloadMap();

    g_kernel->LPrintf( ("Attempting to load: " +file +"...").c_str() );
    // load new scene
    m_currentMap = m_mapImporter.CreateGeom( file );
    if ( m_currentMap ) 
        m_mapLoaded = true;
    else
        m_mapLoaded = false;

    return (m_currentMap == NULL);
}
/**
============================
Unload a scene from memory
============================
*/
void SceneManager::UnloadMap()
{
    if ( m_currentMap ) {
		m_currentMap->FreeGeom();
    }
}
/**
============================
Update the scene
============================
*/
void SceneManager::Update(long dt)
{
    //if ( m_loadMap )
    //{
    //    LoadScene( map_f.m_mapLoadData.nextMap );
    //}
}
/**
============================
Draw the scene
============================
*/
void SceneManager::Render(IRender* r, ICamera* cam)
{
    if ( m_currentMap ) {
        m_currentMap->Render(r, cam );
    }
}



/**
============================
Deconstructor
============================
*/
SceneManager::~SceneManager(void)
{
	UnloadMap();
	// delete the scene
	if ( m_currentMap ) {
		delete m_currentMap;
		m_currentMap = NULL;
	}

    g_kernel->GetConsole()->RemoveCommand( "map" );
}

